﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopNet.Model;

namespace ShopNet
{
    public partial class ListaPedidos : System.Web.UI.Page
    {
        #region Propriedades

        public new Shop Master
        {
            get
            {
                return ((Shop)(base.Master));
            }
        }

        private Usuario Us
        {
            get
            {
                MembershipUser user = Membership.GetUser();
                Usuario us = NServico.Db.Usuarios.FirstOrDefault(u => u.UserId == user.UserName);
                return us;
            }
        }

        protected IList<Pedido> Lista
        {
            get
            {
                if (Session["lista"] == null) Session.Add("lista", new List<Pedido>());
                return (IList<Pedido>)Session["lista"];
            }
            set { if (value != null) Session.Add("lista", value); }
        }

        #endregion

        #region Metodos

        #region CarregarSituacao

        private void CarregarSituacao()
        {
            ddlSitucao.Items.Clear();
            ddlSitucao.Items.Add(new ListItem("Selecione...", "0"));
            var lista = Enum.GetValues(StatusPedido.AguardandoPagamento.GetType());
            foreach (var valor in lista)
            {
                ddlSitucao.Items.Add(new ListItem(valor.ToString(), valor.ToString()));
            }
        }

        #endregion

        #region CarregarGrid

        private void CarregarGrid()
        {
            var lista = NServico.Db.Pedidos.Where(p => p.IdUsuario == Us.IdUsuario);
            if (!string.IsNullOrWhiteSpace(tbUniqIdPedido.Text))
                lista = lista.Where(p => p.IdPedido.ToString() == tbUniqIdPedido.Text);

            if (ddlSitucao.SelectedIndex > 0)
            {
                int statusPed = (int)Enum.Parse(StatusPedido.Pago.GetType(), ddlSitucao.SelectedValue, true);
                lista = lista.Where(p => p.status == statusPed);
            }
            Lista = lista.ToList();
            gvPedidos.DataSource = lista;
            gvPedidos.DataBind();

            if (gvPedidos.Rows.Count > 0)
            {
                //This replaces <td> with <th> and adds the scope attribute
                gvPedidos.UseAccessibleHeader = true;

                //This will add the <thead> and <tbody> elements
                gvPedidos.HeaderRow.TableSection = TableRowSection.TableHeader;

                //This adds the <tfoot> element. 
                //Remove if you don't have a footer row
                //gvPedidos.FooterRow.TableSection = TableRowSection.TableFooter;
                
            }

        }

        #endregion

        #endregion
        
        #region Eventos

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            Master.ExibirMensagem(Shop.TipoMensagem.Success, string.Empty);
            MembershipUser usuario = Membership.GetUser();

            //if (HttpContext.Current.User.Identity.IsAuthenticated)
            if (usuario == null)
                Response.Redirect("~/Checkout/PedidoVerify.aspx", true);

            if (!IsPostBack)
            {
                CarregarSituacao();
                //CarregarGrid();
                
            }
        }
        
        #endregion

        #region gvPedidos_RowDataBound

        protected void gvPedidos_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow) return;
            Label lStatus = (Label) e.Row.FindControl("lStatus");
            Pedido ped = Lista[e.Row.DataItemIndex];
            lStatus.Text = ((StatusPedido) ped.status).ToString();

        }

        #endregion

        #region gvPedidos_RowCommand

        protected void gvPedidos_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Visualizar"))
            {
                Context.Items.Add("IdPedido", int.Parse(e.CommandArgument.ToString()));
                Server.Transfer("~/VisualizarPedido.aspx");
            }
        }

        #endregion

        #region Filtrar_Click

        protected void Filtrar_Click(object sender, EventArgs e)
        {
            CarregarGrid();
        }

        #endregion

        #region gvPedidos_OnPageIndexChanging

        protected void gvPedidos_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPedidos.PageIndex = e.NewPageIndex;
            CarregarGrid();
        }

        #endregion

        #endregion

        

    }
}